//-----------------------------------------------------------------------------
// File: DirectMdiChild.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ObjectiveDraw.Platform
{
	public class DirectMdiChild : Cerebrum.Windows.Forms.DesktopComponent
	{
		public Cerebrum.Direct3D.Forms.DeviceControl DeviceControl;


		private ObjectiveDraw.Platform.KeysInterceptor KeysInterceptor;
		private System.ComponentModel.IContainer components = null;

		public DirectMdiChild(Cerebrum.Windows.Forms.DesktopComponentDisposition relationship):base(relationship)
		{
			this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
			this.SetStyle(System.Windows.Forms.ControlStyles.Selectable, true);
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call

			this.KeysInterceptor.Location = new System.Drawing.Point(-32, -32);

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.KeysInterceptor = new ObjectiveDraw.Platform.KeysInterceptor();
			this.SuspendLayout();
			// 
			// KeysInterceptor
			// 
			this.KeysInterceptor.Location = new System.Drawing.Point(24, 16);
			this.KeysInterceptor.Name = "KeysInterceptor";
			this.KeysInterceptor.Size = new System.Drawing.Size(24, 24);
			this.KeysInterceptor.TabIndex = 0;
			this.KeysInterceptor.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.KeysInterceptor_KeyPress);
			this.KeysInterceptor.KeyUp += new System.Windows.Forms.KeyEventHandler(this.KeysInterceptor_KeyUp);
			this.KeysInterceptor.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeysInterceptor_KeyDown);
			// 
			// DirectMdiChild
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.Add(this.KeysInterceptor);
			this.MinimumSize = new System.Drawing.Size(300, 250);
			this.Name = "DirectMdiChild";
			this.Activated += new System.EventHandler(this.DirectMdiChild_Activated);
			this.ResumeLayout(false);

		}
		#endregion

		/*protected override void OnPaintBackground(PaintEventArgs pevent)
		{
//			base.OnPaintBackground (pevent);
		}*/

		
		/// <summary>
		/// Handle size changed events
		/// </summary>
		protected override void OnSizeChanged(System.EventArgs e)
		{
			this.OnResize(e);
			base.OnSizeChanged(e);
		}


		protected override void OnClick(System.EventArgs e)
		{
			base.OnClick (e);
			if(!this.Focused)
			{
				this.Activate();
				this.Focus();
				Cerebrum.Windows.Forms.Application app = this.DomainContext.Application as Cerebrum.Windows.Forms.Application;
				if(app!=null)
				{
					Cerebrum.Windows.Forms.IPrimaryWindow pw = app.PrimaryWindow as Cerebrum.Windows.Forms.IPrimaryWindow;
					if(pw!=null && pw.ActiveComponent!=this)
					{
						pw.ActiveComponent = this;
					}
				}
			}
		}

		private void KeysInterceptor_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			this.OnKeyDown(e);
		}

		private void KeysInterceptor_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			this.OnKeyPress(e);
		}

		private void KeysInterceptor_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			this.OnKeyUp(e);
		}

		private void DirectMdiChild_Activated(object sender, System.EventArgs e)
		{
			this.KeysInterceptor.Focus();
		}


	}
}

